| Conditions | 1 |
| Paths | 1 |
| Total Lines | 81 |
| Code Lines | 57 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | import { values } from 'ramda'; |
||
| 10 | describe('serverReducer', () => { |
||
| 11 | const list = { |
||
| 12 | abc123: { id: 'abc123' }, |
||
| 13 | def456: { id: 'def456' }, |
||
| 14 | }; |
||
| 15 | const expectedFetchServersResult = { type: FETCH_SERVERS, list }; |
||
| 16 | const ServersServiceMock = { |
||
| 17 | listServers: jest.fn(() => list), |
||
| 18 | createServer: jest.fn(), |
||
| 19 | deleteServer: jest.fn(), |
||
| 20 | createServers: jest.fn(), |
||
| 21 | }; |
||
| 22 | |||
| 23 | describe('reducer', () => { |
||
| 24 | it('returns servers when action is FETCH_SERVERS', () => |
||
| 25 | expect(reducer({}, { type: FETCH_SERVERS, list })).toEqual({ loading: false, list })); |
||
| 26 | }); |
||
| 27 | |||
| 28 | describe('action creators', () => { |
||
| 29 | beforeEach(() => { |
||
| 30 | ServersServiceMock.listServers.mockClear(); |
||
| 31 | ServersServiceMock.createServer.mockReset(); |
||
| 32 | ServersServiceMock.deleteServer.mockReset(); |
||
| 33 | ServersServiceMock.createServers.mockReset(); |
||
| 34 | }); |
||
| 35 | |||
| 36 | describe('listServers', () => { |
||
| 37 | const axios = { get: jest.fn().mockResolvedValue({ data: [] }) }; |
||
| 38 | const dispatch = jest.fn(); |
||
| 39 | |||
| 40 | beforeEach(() => { |
||
| 41 | axios.get.mockClear(); |
||
| 42 | dispatch.mockReset(); |
||
| 43 | }); |
||
| 44 | |||
| 45 | it('fetches servers from local storage when found', async () => { |
||
| 46 | await listServers(ServersServiceMock, axios)()(dispatch); |
||
| 47 | |||
| 48 | expect(dispatch).toHaveBeenCalledTimes(2); |
||
| 49 | expect(dispatch).toHaveBeenNthCalledWith(1, { type: FETCH_SERVERS_START }); |
||
| 50 | expect(dispatch).toHaveBeenNthCalledWith(2, expectedFetchServersResult); |
||
| 51 | expect(ServersServiceMock.listServers).toHaveBeenCalledTimes(1); |
||
| 52 | expect(ServersServiceMock.createServer).not.toHaveBeenCalled(); |
||
| 53 | expect(ServersServiceMock.deleteServer).not.toHaveBeenCalled(); |
||
| 54 | expect(ServersServiceMock.createServers).not.toHaveBeenCalled(); |
||
| 55 | expect(axios.get).not.toHaveBeenCalled(); |
||
| 56 | }); |
||
| 57 | |||
| 58 | it('tries to fetch servers from remote when not found locally', async () => { |
||
| 59 | const NoListServersServiceMock = { ...ServersServiceMock, listServers: jest.fn(() => ({})) }; |
||
| 60 | |||
| 61 | await listServers(NoListServersServiceMock, axios)()(dispatch); |
||
| 62 | |||
| 63 | expect(dispatch).toHaveBeenCalledTimes(2); |
||
| 64 | expect(dispatch).toHaveBeenNthCalledWith(1, { type: FETCH_SERVERS_START }); |
||
| 65 | expect(dispatch).toHaveBeenNthCalledWith(2, { type: FETCH_SERVERS, list: {} }); |
||
| 66 | expect(NoListServersServiceMock.listServers).toHaveBeenCalledTimes(1); |
||
| 67 | expect(NoListServersServiceMock.createServer).not.toHaveBeenCalled(); |
||
| 68 | expect(NoListServersServiceMock.deleteServer).not.toHaveBeenCalled(); |
||
| 69 | expect(NoListServersServiceMock.createServers).toHaveBeenCalledTimes(1); |
||
| 70 | expect(axios.get).toHaveBeenCalledTimes(1); |
||
| 71 | }); |
||
| 72 | }); |
||
| 73 | |||
| 74 | describe('createServer', () => { |
||
| 75 | it('adds new server and then fetches servers again', () => { |
||
| 76 | const serverToCreate = { id: 'abc123' }; |
||
| 77 | const result = createServer(ServersServiceMock, () => expectedFetchServersResult)(serverToCreate); |
||
| 78 | |||
| 79 | expect(result).toEqual(expectedFetchServersResult); |
||
| 80 | expect(ServersServiceMock.createServer).toHaveBeenCalledTimes(1); |
||
| 81 | expect(ServersServiceMock.createServer).toHaveBeenCalledWith(serverToCreate); |
||
| 82 | expect(ServersServiceMock.listServers).not.toHaveBeenCalled(); |
||
| 83 | expect(ServersServiceMock.deleteServer).not.toHaveBeenCalled(); |
||
| 84 | expect(ServersServiceMock.createServers).not.toHaveBeenCalled(); |
||
| 85 | }); |
||
| 86 | }); |
||
| 87 | |||
| 88 | describe('deleteServer', () => { |
||
| 89 | it('deletes a server and then fetches servers again', () => { |
||
| 90 | const serverToDelete = { id: 'abc123' }; |
||
| 91 | const result = deleteServer(ServersServiceMock, () => expectedFetchServersResult)(serverToDelete); |
||
| 117 |